package com.yang.util;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;


public class TransformUtil {
	
	public static JSONObject map2Json(Map<String, String> map){
		JSONObject jObj = new JSONObject();
		
		Set<String> set = map.keySet();
		Iterator<String> iter = set.iterator();
		
		while(iter.hasNext()){
			String key = iter.next();
			try {
				jObj.put(key, map.get(key));
			} catch (JSONException e) {
				e.printStackTrace();
				return null;
			}
		}
		return jObj;
	}
	
	public static JSONArray list2JsonArray(List<Map<String, String>> list){
		JSONArray jsonArr = new JSONArray();
		
		for(int i=0; i<list.size(); i++){
			Map<String, String> map = list.get(i);
			JSONObject jObj = map2Json(map);
			jsonArr.add(jObj);
		}
		return jsonArr;
	}
}
